package com.cll.learn.control.demo02;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 描述信息:
 * 主线程通知下面的任务，同时开始
 * @author CLL
 * @version 1.0
 * @date 2021/5/15 17:03
 */
public class CountDownLatchDemo02 {

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        Thread thread1 = new Thread(new ConcurrentThread(countDownLatch));
        thread1.start();
        try {
            Thread.sleep(new Random().nextInt(100));
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread thread2 = new Thread(new ConcurrentThread(countDownLatch));
        thread2.start();
        try {
            Thread.sleep(new Random().nextInt(100));
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread thread3 = new Thread(new ConcurrentThread(countDownLatch));
        thread3.start();
        try {
            Thread.sleep(new Random().nextInt(100));
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread thread4 = new Thread(new ConcurrentThread(countDownLatch));
        thread4.start();
        try {
            Thread.sleep(new Random().nextInt(100));
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread thread5 = new Thread(new ConcurrentThread(countDownLatch));
        thread5.start();
        try {
            Thread.sleep(new Random().nextInt(100));
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            thread1.join();
            thread2.join();
            thread3.join();
            thread4.join();
            thread5.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程结束");
    }
}
